import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Tabs} from '../../tabs/tabs';
import {Storage} from '@ionic/storage';

import {dialogUtil} from "../../../service/dialogUtil";
import {httpService} from "../../../service/httpService";
import {HOST_URL} from "../../../service/Constants";
import {FindPwd} from '../findPwd/findPwd';
import {Register} from '../register/register';

/**
 * Generated class for the LoginPage page.
 *
 */
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class Login {

  constructor(public navCtrl: NavController,
              public storage: Storage,
              private dialog: dialogUtil,
              private http: httpService,
  ) {

  }

  userInfo: UserInfo = {userCode: '', password: ''};
  //登录
  btnClick() {

    if (this.userInfo.userCode != "" && this.userInfo.password != "") {
    this.http.getLogin(HOST_URL+'/api/v1/user/login/?filter=',{userCode:this.userInfo.userCode,userPwd:this.userInfo.password,__s:1,__r:0}).then(res=>{
      alert(JSON.stringify(res))
      if(res.error.code=='0'){
        //标识登录状态
        this.storage.set("isLogin", true);
        //存入全局用户信息
        this.storage.set('userInfo', res.rows[0]);
        this.storage.set('token', res.token);
        this.toIndexPage()
      }else if(res.error.code=='801'){
        this.dialog.showAlert("账号或密码有误。");
      }
      })
    }
    else
      this.dialog.showAlert("账号和密码不能为空！");
  }

  /**
   * 跳转首页
   */
  toIndexPage() {
    this.navCtrl.push(Tabs);
  }

  //跳转到找回密码
  toFindPwd() {
    this.navCtrl.push(FindPwd);
  }

  //注册
  toRegister() {
    this.navCtrl.push(Register);
  }
}

export class UserInfo {
  userCode: string;
  password: string;
}
